< previous page page_100 next page >

Page 100
The next four lines of code in the adjustBalance method are important. The LocalObject object variable is initialized to whatever object is represented by the argObject argument variable. Then LocalObject is passed to two private methods, increaseFunds and decreaseFunds, for special processing. Notice the seeming ambiguity of first increasing the funds and then decreasing the funds. On closer inspection, you'll find that there is no ambiguity at all. Only one method can handle the LocalObject message. Look at the bodies of these two private methods. As with the adjustBalance method, these two check to make sure that each can handle the incoming message. Because the LocalObject variable was set to the theDeposit object (via argObject), only increaseBalance can handle it, whereas decreaseBalance ignores it. Why do this? Because it avoids the unnecessary introduction of a complex IfThen statement. That is, if you converted the following two lines of code
Call increaseFunds(LocalObject)
Call decreaseFunds(LocalObject)
to the following:
If TypeName(argObject) = Deposit Then

     'It's a Deposit object
     CurrentBalance = CurrentBalance + argObject.Amount

ElseIf TypeName(argObject) = Withdrawal Then

     'It's a Withdrawal object
     CurrentBalance = CurrentBalance - argObject.Amount

Else

     Exit Function

End If
it's harder to readdespite the two styles being functionally equivalent. Having such method calls avoids the clumsiness of complex IfThen statements and makes it easier to maintain nicely encapsulated chunks of code. Finally, the two methods exhibit the behavior expected by the client: either to increment or decrement the CurrentBalance attribute by the value of the Amount property of the argObject object.
Tip
Having precondition checks is important in interobject communications. They ensure that the object is the proper recipient of a message. The idea of precondition checks is part of what's called self-testing objects. You will read more about this concept on Day 8, Versioning and Debugging.

 
< previous page page_100 next page >

If you like this book, buy it!